package com.example.blog1.config;

import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import com.example.blog1.utils.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("程序执行前");
        String token = request.getHeader("user_token");
        Claims claims = JwtUtil.parseJwt(token);
        if (claims == null) {
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
